import { _decorator, Component, Node, Prefab, UITransform } from 'cc';
const { ccclass, property } = _decorator;
import { BaseComponent } from "./BaseComponent";

@ccclass('UIRoot')
/**主场景UI根节点 */
export abstract class UIRoot extends BaseComponent {

    /**视图层根节点 */
    @property(Node)
    viewRoot: Node = null

    /**显示在视图层上面的提示层 */
    @property(Node)
    tipRoot: Node = null

    /**强制屏蔽点击事件的节点*/
    // @property(Node)
    // blockNode: Node = null

    /**转圈等待的屏蔽点击节点*/
    @property(Node)
    waittingBlockNode: Node = null

    /**黑背景 */
    @property(Node)
    blackBg: Node = null

    touckBlackBgCbk: Function = null

    onTouchBlackBg() {
        this.touckBlackBgCbk && this.touckBlackBgCbk()
    }

    layerToIndexMap: Map<Node, number> = new Map()

    indexToLayerMap: Map<number, Node> = new Map()

    getTipLayer(layerIndex: number): Node {
        let node = this.indexToLayerMap.get(layerIndex)
        if (!node) {
            node = new Node()
            node.addComponent(UITransform)
            this.tipRoot.addChild(node)
            this.indexToLayerMap.set(layerIndex, node)
            this.layerToIndexMap.set(node, layerIndex)

            this.tipRoot.children.sort((a, b) => {
                return this.layerToIndexMap.get(a) - this.layerToIndexMap.get(b)
            })
        }
        return node
    }

    addTip(node: Node, layerIndex: number) {
        let layer = this.getTipLayer(layerIndex)
        layer.addChild(node)
    }
}

